package com.nit.controller;


import com.alibaba.fastjson.JSONObject;
import com.nit.dto.UserDTO;
import com.nit.dto.utils.OpResultDTO;
import com.nit.service.UserService;
import com.nit.utils.MD5Util;
import com.nit.utils.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/api/system")
public class SystemController {

    @Resource
    private UserService userService;
    private final Logger logger = LoggerFactory.getLogger(UserController.class);


    /**
     * @Description: 登录
     * @Author: 张凡
     * @Date: 2019/9/8 17:07
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public JSONObject loginCheck(@RequestBody UserDTO userDTO) {
        JSONObject loginResponse = new JSONObject();
        try {
            loginResponse = userService.loginCheck(
                    userDTO.getUserName(), MD5Util.getMD5(userDTO.getUserPwd(), true, 16),
                    userDTO.getVerificationCode(), userDTO.getUcId());
        } catch (Exception e) {
            logger.error(e.toString());
            loginResponse.put("status", "error");
            loginResponse.put("reason", "服务器出错啦!");
        }
        return loginResponse;
    }

    /**
     * @Description: 注册
     * @Author: 张凡
     * @Date: 2019/9/8 17:07
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public OpResultDTO register(@RequestBody UserDTO userDTO) {
        OpResultDTO opResultDTO = new OpResultDTO(SystemConstant.OP_FAILURE_LONG, SystemConstant.OP_FAILURE_OBJ);
        try {
            opResultDTO = userService.register(userDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L)
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        return opResultDTO;
    }

    /**
     * @Description: 获取验证码
     * @Author: 张凡
     * @Date: 2019/9/8 16:31
     */
    @RequestMapping(value = "/getUserCode", method = RequestMethod.POST)
    public OpResultDTO getUserCode() {
        OpResultDTO opResultDTO = new OpResultDTO();
        try {
            opResultDTO = userService.getUserCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return opResultDTO;
    }
}
